package com.wf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wf.context.BaseContext;
import com.wf.domain.MyFavorites;
import com.wf.dto.PageDTO;
import com.wf.mapper.HousesInfoMapper;
import com.wf.service.MyFavoritesService;
import com.wf.mapper.MyFavoritesMapper;
import com.wf.vo.HouseDetailsPageVO;
import com.wf.vo.PageResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 针对表【my_favorites】的数据库操作Service实现
 */
@Service
public class MyFavoritesServiceImpl extends ServiceImpl<MyFavoritesMapper, MyFavorites>
        implements MyFavoritesService {

    @Autowired
    private HousesInfoMapper housesInfoMapper;

    /**
     * 根据用户ID查询其收藏的房源信息
     *
     * @param pageDTO 分页查询对象，包含分页参数
     * @return 包含房源详细信息的分页结果对象
     */
    @Override
    public PageResultVO<HouseDetailsPageVO> getList(PageDTO pageDTO) {
        // 根据用户ID查询收藏记录
        LambdaQueryWrapper<MyFavorites> wrapper = new LambdaQueryWrapper<MyFavorites>()
                .eq(MyFavorites::getUserId, BaseContext.getCurrentId());
        List<MyFavorites> list = this.list(wrapper);
        PageResultVO<HouseDetailsPageVO> resultVO = new PageResultVO<>();
        if (list.isEmpty()) {
            return resultVO;
        }

        // 提取收藏记录中的房源ID
        List<Integer> contractIds = list.stream().map(MyFavorites::getContractId).collect(Collectors.toList());
        pageDTO.setContractIds(contractIds);

        // 根据房源ID查询房源详细信息
        List<HouseDetailsPageVO> houseDetailsPageVOS = housesInfoMapper.selectpageList(pageDTO);

        // 构建并返回分页结果对象
        resultVO.setList(houseDetailsPageVOS);
        resultVO.setTotal(contractIds.size());
        return resultVO;
    }
}
